﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Sockets;
using CommonDomainInterfaces;
using System.Drawing;


namespace ServerConsole
{
    class ClientsMonitor
    {
        private List<BinaryWriter> writers;
        private object oLock = new object();
        ServerToKlientCommands commands = new ServerToKlientCommands();

        public ClientsMonitor()
        {
            writers = new List<BinaryWriter>();
        }

        public void AddWriter(BinaryWriter writer)
        {
            lock (oLock)
            {
                writers.Add(writer);
            }
        }

        public void RemoveWriter(BinaryWriter writer)
        {
            lock (oLock)
            {
                writers.Remove(writer);
            }
        }

        public void BroadCastNewLot(string condition, int age, decimal ReservePrice, decimal Valuation, Image image, string AuctionDate, string Description, string Category, string LotNumber)
        {
            foreach (BinaryWriter bw in writers)
            {
                ImageConverter converter = new ImageConverter();

                byte[] imagearray = (byte[])converter.ConvertTo(image, typeof(byte[]));

                byte commandtosend = (byte)ServerToKlientCommands.SendLotInfo;
                bw.Write(commandtosend);
                bw.Write(condition);
                bw.Write(age);
                bw.Write(ReservePrice);
                bw.Write(Valuation);
                bw.Write(imagearray);
                bw.Write(AuctionDate);
                bw.Write(Description);
                bw.Write(Category);
                bw.Write(LotNumber);
            }
        }

        public void BroadCastNewHighestBid(decimal amount, string name)
        {
            foreach (BinaryWriter bw in writers)
            {
                bw.Write((byte)ServerToKlientCommands.UpdateHighestBid);
                bw.Write(name);
            }
        }

        public void BroadCastHammerStroke()
        {

        }

        public void BroadCastAuctionStarted()
        {

        }

        public void BroadCastAuctionEnded()
        {

        }
    }
}